package com.git.contractmanagement.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.git.contractmanagement.entity.User;
import com.git.contractmanagement.mapper.UserMapper;
import com.git.contractmanagement.service.UserService;
import com.git.contractmanagement.util.PasswordUtil;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User login(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = this.getOne(wrapper);
        // 使用 PasswordUtil.matches() 验证密码
        if (user == null || !PasswordUtil.matches(password, user.getPassword())) {
            return null;
        }

        return user;
    }

    @Override
    public String getRoleByUserId(Long userId) {
        User user = this.getById(userId);
        return user != null ? user.getRole() : null;
    }
}
